import { constantRoutes, asyncRoutes } from '@/router'

// 处理页面权限
const state = () => ({
  routes: constantRoutes // 默认值是静态路由，最终是静态路由+用户的动态路由
})

const mutations = {
  // 设置router（data就是筛选处理好的用户的动态路由）
  setRoutes(state, data) {
    state.routes = [...constantRoutes, ...data]
  }
}

const actions = {
  // 根据用户的页面权限标识本地完整的动态路由筛选
  filterRoutes({ commit }, mark) {
    const userAsyncRoutes = []
    asyncRoutes.forEach(item => {
      if (mark.includes(item.name)) userAsyncRoutes.push(item)
    })
    commit('setRoutes', userAsyncRoutes)
    return userAsyncRoutes
  }
}

// 暴露出去
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
